package com.mocoder.chatdemo.controller;

import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

import com.mocoder.chatdemo.model.UserInfo;
import com.mocoder.chatdemo.protocol.ProtocolHelper;
import com.mocoder.chatdemo.view.UIHelper;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TabPane;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class MainController {

    @FXML
    public TabPane chatPane;
    @FXML
    private VBox friendsListView;

    private Map<String, Label> friendMap = new HashMap<>();

    public void updateFriendList(InetAddress addr, UserInfo userInfo) {
        UserInfo oldUserInfo = ProtocolHelper.getUserInfo(addr);
        if (oldUserInfo == null || !friendMap.containsKey(addr.getHostAddress())) {
            ProtocolHelper.setUserInfo(addr, userInfo);
            ImageView imageView = new ImageView(userInfo.genPhotoImage());
            imageView.setFitHeight(30);
            imageView.setFitWidth(30);
            imageView.setPreserveRatio(true);
            Label label = new Label(ProtocolHelper.getUserName(addr));
            label.setMaxHeight(30);
            label.setGraphic(imageView);
            label.setGraphicTextGap(5);
            label.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    UIHelper.createChatFriendTab(chatPane, addr);
                }
            });
            friendsListView.getChildren().add(label);
            friendMap.put(addr.getHostAddress(), label);
        } else if (userInfo.getUpdateTime() > oldUserInfo.getUpdateTime()) {
            Label label = friendMap.get(addr.getHostAddress());
            label.setText(userInfo.getName());
            ((ImageView)label.getGraphic()).setImage(userInfo.genPhotoImage());
            ProtocolHelper.setUserInfo(addr, userInfo);
        }
    }

    public void btn_flushFriendsList(ActionEvent actionEvent) {
        friendsListView.getChildren().clear();
        friendMap.clear();
        ProtocolHelper.sendUserScanBroadcase(ProtocolHelper.getUserInfo(null));
    }

    public void onUILoad() {
        btn_flushFriendsList(null);
    }

    public void chatRoomClick(MouseEvent mouseEvent) {
        UIHelper.createChatRoomTab(chatPane);
    }

    public void addChatMsg(InetAddress msgFrom, String msg, boolean isChatRoom) {
        UIHelper.addChatRecord(chatPane, msgFrom, msg, isChatRoom);
    }

    private Stage setupStage;

    public void btn_setup(ActionEvent actionEvent) {
        if (setupStage == null) {
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("../setup.fxml"));
                Parent root = loader.load();
                setupStage = new Stage();
                Scene scene = new Scene(root, 500, 400);
                setupStage.setScene(scene);
                setupStage.setTitle("设置");
                setupStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                    @Override
                    public void handle(WindowEvent event) {
                        event.consume();
                        setupStage.hide();
                    }
                });
                setupStage.setOnShown(new EventHandler<WindowEvent>() {
                    @Override
                    public void handle(WindowEvent event) {
                        SetupController controller = loader.getController();
                        controller.onUILoad();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        setupStage.show();
    }

}
